package union

import (
	log2 "github.com/name5566/leaf/log"
	"go.mongodb.org/mongo-driver/bson"
	"server/db"
)

type UnionDao struct {
	db *db.MongoMiddleware
}

// Constants
const (
	TBL_UNION            = "t_union"
	TBL_USER_BUILD_UNION = "t_user_union"
	TBL_UNIONSHOP        = "t_union_shop"
	// Max allowed data per query
	DATA_MAX_IN = 100
)

func NewUnionDao() *UnionDao {
	return &UnionDao{
		db: db.Db,
	}
}
func (v *UnionDao) getUnionShop(userId string) (UnionShop, error) {
	var result UnionShop
	filter := bson.M{
		"user_id": userId,
	}
	err := db.Db.FindOne(TBL_UNIONSHOP, filter, &result)
	if err != nil {
		return result, err
	}
	return result, nil
}
func (v *UnionDao) buy(userId string, lingQuTimes int) error {
	filter := bson.M{
		"user_id": userId,
	}
	update := bson.M{
		"$set": bson.M{
			"ling_qu_times": lingQuTimes,
		},
	}
	err := db.Db.Update(TBL_UNIONSHOP, filter, update)
	if err != nil {
		return err
	}
	return nil
}
func (v *UnionDao) Build(userId string, buildType, buildTime int, CDTime int64) error {
	filter := bson.M{
		"user_id":    userId,
		"build_type": buildType,
	}
	update := bson.M{
		"$set": bson.M{
			"build_time": buildTime,
		},
	}
	err := db.Db.Update(TBL_USER_BUILD_UNION, filter, update)
	if err != nil {
		return err
	}
	return nil
}
func (v *UnionDao) BuildInfo(userId string, buildType int) (BuildUnion, error) {
	var info BuildUnion
	filter := bson.M{
		"user_id":    userId,
		"build_type": buildType,
	}
	err := db.Db.Update(TBL_USER_BUILD_UNION, filter, &info)
	if err != nil {
		return info, err
	}
	return info, nil
}
func (v *UnionDao) BuildList(userId string) ([]BuildUnion, error) {
	var list []BuildUnion
	filter := bson.M{
		"user_id": userId,
	}
	err := db.Db.Find(TBL_USER_BUILD_UNION, filter, &list)
	if err != nil {
		return list, err
	}
	return list, nil
}
func (v *UnionDao) insertUnionBuild(userId string, build BuildUnion) error {
	build.UserId = userId
	return db.Db.Insert(TBL_USER_BUILD_UNION, build)

}
func (v *UnionDao) insertShop(userId string, shop UnionShop) error {
	shop.UserId = userId
	return db.Db.Insert(TBL_UNIONSHOP, shop)

}
func (v *UnionDao) upBuildValue(unionId int32, BuildDu int) error {
	filter := bson.M{
		"union_id": unionId,
	}
	update := bson.M{
		"$set": bson.M{
			"build_du": BuildDu,
		},
	}
	return db.Db.Update(TBL_UNION, filter, update)

}
func (v *UnionDao) getUnionInfo(unionId int32) (Union, error) {
	var result Union
	filter := bson.M{
		"union_id": unionId,
	}
	err := db.Db.FindOne(TBL_UNION, filter, &result)
	if err != nil {
		return result, err
	}
	return result, nil
}
func (v *UnionDao) getUnionInfoByUnionId(unionId int32) (Union, error) {
	var result Union
	filter := bson.M{
		"union_id": unionId,
	}
	err := db.Db.FindOne(TBL_UNION, filter, &result)
	if err != nil {
		log2.Error("错误请求 %s", err.Error())
		return result, err
	}
	return result, nil
}
func (v *UnionDao) getUnionList(userId string) ([]Union, error) {
	var result []Union
	filter := bson.M{
		"master": userId,
	}
	err := db.Db.Find(TBL_UNION, filter, &result)
	if err != nil {
		return result, err
	}
	return result, nil
}
func (v *UnionDao) insert(userId string, union Union) error {
	union.Master = userId
	return db.Db.Insert(TBL_UNION, union)

}
func (v *UnionDao) upUnionLevel(userId string, unionId int32, level int32) error {
	filter := bson.M{
		"union_id": unionId,
	}
	update := bson.M{
		"$set": bson.M{
			"union_Level": level,
		},
	}
	err := db.Db.Update(TBL_UNION, filter, update)
	if err != nil {
		return err
	}

	return nil
}

func (v *UnionDao) joinUnion(unionId int32, renYuanList []UnionUserInfo) error {
	filter := bson.M{
		"union_id": unionId,
	}
	update := bson.M{
		"$set": bson.M{
			"re_yuan_list": renYuanList,
		},
	}
	err := db.Db.Update(TBL_UNION, filter, update)
	if err != nil {
		return err
	}

	return nil
}
func (v *UnionDao) upUserLevel(userId string, unionId int32, renYuanList []UnionUserInfo) error {
	filter := bson.M{
		"user_id":  userId,
		"union_id": unionId,
	}
	update := bson.M{
		"$set": bson.M{
			"re_yuan_list": renYuanList,
		},
	}
	err := db.Db.Update(TBL_UNION, filter, update)
	if err != nil {
		return err
	}

	return nil
}
func (v *UnionDao) baiFang(unionId int32, renYuanList []UnionUserInfo) error {
	filter := bson.M{
		"union_id": unionId,
	}
	update := bson.M{
		"$set": bson.M{
			"re_yuan_list": renYuanList,
		},
	}
	err := db.Db.Update(TBL_UNION, filter, update)
	if err != nil {
		return err
	}

	return nil
}
